
 
/**
 * 动态创建iframe
 * @param dom 创建iframe的容器，即在dom中创建iframe。dom可以是div、span或者其他标签。
 * @param src iframe中打开的网页路径
 * @param onload iframe加载完后触发该事件，可以为空
 * @return 返回创建的iframe对象
 */
function createIframe(dom, src, onload,id) {
    //在document中创建iframe
    var iframe = document.createElement("iframe");
    iframe.id = id|| "viewFrame";

    //绑定iframe的onload事件
    if (onload && Object.prototype.toString.call(onload) === '[object Function]') {
        if (iframe.attachEvent) {
            iframe.attachEvent('onload', onload);
        } else if (iframe.addEventListener) {
            iframe.addEventListener('load', onload);
        } else {
            iframe.onload = onload;
        }
    }
    src = src + CsrfGuard.getUrlSurffix(src);
    iframe.src = src;
    iframe.height ="100%";
    iframe.width ="100%";
    iframe.frameBorder="0";
    iframe.scrolling="no";
    //把iframe加载到dom下面
    dom.appendChild(iframe);
    return iframe;
}

function destroyAndCreateIframe(dom, src, onload,id,iframe){
    destroyIframe(iframe);
    createIframe(dom, src, onload,id)

}

/**
 * 销毁iframe，释放iframe所占用的内存。
 * @param iframe 需要销毁的iframe对象
 */
function destroyIframe(iframe) {
    //把iframe指向空白页面，这样可以释放大部分内存。
    iframe.src = 'about:blank';
    try {
        iframe.contentWindow.document.write('');
        iframe.contentWindow.document.clear();
    } catch (e) {}
    //把iframe从页面移除
    iframe.parentNode.removeChild(iframe);
}

// var newWinObj = null;
var newWinReport = null;
//别问我为啥这么搞...为了兼容IE
var newWinObjArr = {
    locations: [],
    openWindow: []
};

function openNewWin(url, s, p, fn) {
    try {
        var flag = false;
        for (var i = newWinObjArr.locations.length - 1; i >= 0; i--) {
            if (!newWinObjArr.openWindow[i].closed &&newWinObjArr.locations[i]  == url) {
                var random = $.messageBox({
                    'imgType': 2,
                    'type': 0,
                    'msg': '${ctp:i18n("cap4.form.business.formpageopend")}',
                    close_fn: function () {
                        newWinObjArr.openWindow[i].focus();
                    },
                    ok_fn: function () {
                        newWinObjArr.openWindow[i].focus();
                    }
                })
                flag = true;
                break;
            }
        }
        if (!flag) {
            var newWinObj = null;
            window.onCloseFormDesign = fn;
            newWinObj = window.open(url, "", p);
            newWinObj.moveTo(0, 0);
            newWinObj.resizeTo(screen.availWidth, screen.availHeight);
            newWinObj.focus();
            newWinObjArr.locations.push(url);
            newWinObjArr.openWindow.push(newWinObj);
        }

    } catch (e) {

    }
}

function openNewReport(url, s, p, fn) {
    try {
        if (window.opener && window.opener == newWinReport) {
            window.opener.location = url;
        }
        //以下代码实现 只打开一个窗口
        else if (newWinReport == null || newWinReport.closed) {
            newWinReport = window.open(url, s, p);
            newWinReport.moveTo(0, 0);
            newWinReport.resizeTo(screen.width, screen.height);
        } else {
            var random = $.messageBox({
                'imgType': 2,
                'type': 0,
                'msg': '${ctp:i18n("cap4.form.business.reportpageopend")}',
                close_fn: function () {
                    newWinReport.focus();
                },
                ok_fn: function () {
                    newWinReport.focus();
                }
            })
            // newWinObj.location.href = url;
        }
    } catch (e) {

    }
}

function setBizConfig(sID, sName,isOwner,isBaseApp) {
    bizConfig = {};
    bizConfig.id = sID;
    bizConfig.name = sName;
    bizConfig.isOwner = isOwner?1:0;
    bizConfig.isBaseApp=isBaseApp?1:0;
}
//菊花图
function spinning(flag) {
    if(!$(top.document).find("#loading").length){
        var loadhtml = '<div style="position: fixed;z-index: 999;top: 0;left: 0;overflow: hidden;width: 100%;height: 100%;text-align: center;" id="loading">';
            loadhtml+='<div style=" position: absolute;top: 0;left: 0;width: 100%;height: 100%;opacity: .4;background: #000;"></div>';
            loadhtml+='<div style="position: relative;top: 40%;z-index:1000;display: inline-block;">';
            loadhtml+='<img src="${path}/common/cap4/bizconfig/images/loading.gif" /></div></div>'
        $(top.document).find("body").append(loadhtml);
    }
    $(top.document).find("#loading")[flag ? 'show' : 'hide']();
}

function escapeHtml(string) {
    var entityMap = {
        "&": "&amp;",
        "<": "&lt;",
        ">": "&gt;",
        '"': '&quot;',
        "'": '&#39;',
        "/": '&#x2F;'
    };
    return String(string).replace(/[&<>"'\/]/g, function (s) {
        return entityMap[s];
    });
}

//刷新v5菜单
function refreshCap4Menu() {
    if (window.opener && window.opener.refreshCurrentSpaceMenuNavForPortal) {
        window.opener.refreshCurrentSpaceMenuNavForPortal(true);
    }
}


function getDataDic(id) {

    var dictionary = callBackendMethod("cap4FormListManager", "getDataDictionary", id);
    if (dictionary) {
        var dialog = $.dialog({
            html: getHtml(dictionary),
            title: "${ctp:i18n('form.base.datadictionary')}",
            width: 700,
            height: 560,
            isDrag: true,
            checkMax: true,
            buttons: [{
                text: dictionary.keys.close,
                isEmphasize: true,
                handler: function () {
                    dialog.close()
                }
            }]
        });
    }

    function getHtml(data) {
        var html = [];

        html.push("<div style='user-select: text;'>");

        var info = [];
        info.push("  <fieldset style='margin:20px;'><legend>" + data.keys.formInfo + "</legend>");
        info.push("  <div id='info'>");
        info.push("    <table cellspacing='0' cellpadding='0'>");
        info.push("      <tr><th style='font-weight: bold;height: 32px;'>" + data.keys.formName + "</th><th>" +
            data.formInfo.formName + "</th></tr>");
        info.push("      <tr><th style='font-weight: bold;height: 32px;'>" + data.keys.categoryName +
            "</th><th>" +
            data.formInfo.categoryName + "</th></tr>");
        info.push("      <tr><th style='font-weight: bold;height: 32px;'>" + data.keys.formTypeName +
            "</th><th>" +
            data.formInfo.formTypeName + "</th></tr>");
        info.push("      <tr><th style='font-weight: bold;height: 32px;'>" + data.keys.creatorName +
            "</th><th>" +
            data.formInfo.creatorName + "</th></tr>");
        info.push("      <tr><th style='font-weight: bold;height: 32px;'>" + data.keys.ownerName + "</th><th>" +
            data.formInfo.ownerName + "</th></tr>");
        info.push("    </table>");
        info.push("  </div>");
        info.push("  </fieldset>");

        html.push(info.join(""));

        var master = [];
        master.push("  <fieldset style='margin:20px;'><legend>" + data.keys.masterInfo + "</legend>");
        master.push("  <div id='master'>");
        master.push("    <table border='0'>");
        master.push("      <tr><th style='font-weight: bold;height: 32px;'>" + data.keys.tableName +
            "</td><td>&nbsp;</td><td colspan='5'>" + data.mList[0].Fields.mTableDisplay + "</td></tr>");
        master.push("      <tr><th style='font-weight: bold;height: 32px;'>" + data.keys.DBtableName +
            "</td><td>&nbsp;</td><td colspan='5'>" + data.mList[0].Fields.mTableName + "</td></tr>");
        master.push("      <tr><th style='font-weight: bold;height: 32px;' align='center'>" + data.keys.fieldName +
            "</td><td>&nbsp;</td><th style='font-weight: bold;height: 32px;' align='center'>" + data.keys.fieldType +
            "</td><td>&nbsp;</td><th style='font-weight: bold;height: 32px;' align='center'>" + data.keys.fieldLength +
            "</td><td>&nbsp;</td><th style='font-weight: bold;height: 32px;' align='center'>" + data.keys.fieldDisplay +
            "</td><td>&nbsp;</td><th style='font-weight: bold;height: 32px;' align='center'>" + data.keys.finalInputType +
            "</td><td>&nbsp;</td><th style='font-weight: bold;height: 32px;' align='center'>" + data.keys.finalFieldType +
            "</td></tr>");
        var masterfield = [];
        var mfields = data.mList[0].Fields.mFieldsInfo
        for (var i = 0; i < mfields.length; i++) {
            masterfield.push("   <tr>")
            masterfield.push("     <td align='center'>" + mfields[i].name + "</td><td>&nbsp;</td>");
            masterfield.push("     <td align='center'>" + mfields[i].fieldType + "</td><td>&nbsp;</td>");
            masterfield.push("     <td align='center'>" + mfields[i].fieldLength + "</td><td>&nbsp;</td>");
            masterfield.push("     <td align='center'>" + mfields[i].display + "</td><td>&nbsp;</td>");
            masterfield.push("     <td align='center'>" + mfields[i].finalInputType + "</td><td>&nbsp;</td>");
            masterfield.push("     <td align='center'>" + mfields[i].finalFieldType + "</td>");
            masterfield.push("   </tr>");
        }
        master.push(masterfield.join(""));
        master.push("    </table>");
        master.push("  </div>");
        master.push("  </fieldset>");

        html.push(master.join(""));

        var subtable = data.sList
        if (subtable) {
            var sub = [];
            sub.push("  <fieldset style='margin:20px;'><legend>" + data.keys.subInfo + "</legend>");
            sub.push("  <div id='sub'>");

            for (var j = 0; j < subtable.length; j++) {
                var subtablefield = subtable[j].Fields;
                sub.push("    <br/><table border='0'>");
                sub.push("      <tr><th style='font-weight: bold;height: 32px;'>" + data.keys.tableName +
                    "</th><td>&nbsp;</td><td colspan='5'>" + subtable[j].Fields.sTableDisplay +
                    "</td></tr>");
                sub.push("      <tr><th style='font-weight: bold;height: 32px;'>" + data.keys.subTableCreateTime +
                    "</th><td>&nbsp;</td><td colspan='5'>" + subtable[j].Fields.createDate +
                    "</td></tr>");
                sub.push("      <tr><th style='font-weight: bold;height: 32px;'>" + data.keys.DBtableName +
                    "</td><td>&nbsp;</td><td colspan='5'>" + subtable[j].Fields.sTableName + "</td></tr>");
                sub.push("      <tr><th style='font-weight: bold;height: 32px;' align='center'>" + data.keys.fieldName +
                    "</td><td>&nbsp;</td><th style='font-weight: bold;height: 32px;' align='center'>" +
                    data.keys
                    .fieldType +
                    "</td><td>&nbsp;</td><th style='font-weight: bold;height: 32px;' align='center'>" +
                    data.keys
                    .fieldLength +
                    "</td><td>&nbsp;</td><th style='font-weight: bold;height: 32px;' align='center'>" +
                    data.keys
                    .fieldDisplay +
                    "</td><td>&nbsp;</td><th style='font-weight: bold;height: 32px;' align='center'>" +
                    data.keys
                    .finalInputType +
                    "</td><td>&nbsp;</td><th style='font-weight: bold;height: 32px;' align='center'>" +
                    data.keys
                    .finalFieldType + "</td></tr>");
                var sfields = subtablefield.sFieldsInfo;
                for (var k = 0; k < sfields.length; k++) {
                    sub.push("   <tr>")
                    sub.push("     <td align='center'>" + sfields[k].name + "</td><td>&nbsp;</td>");
                    sub.push("     <td align='center'>" + sfields[k].fieldType + "</td><td>&nbsp;</td>");
                    sub.push("     <td align='center'>" + sfields[k].fieldLength + "</td><td>&nbsp;</td>");
                    sub.push("     <td align='center'>" + sfields[k].display + "</td><td>&nbsp;</td>");
                    sub.push("     <td align='center'>" + sfields[k].finalInputType + "</td><td>&nbsp;</td>");
                    sub.push("     <td align='center'>" + sfields[k].finalFieldType + "</td>");
                    sub.push("   </tr>");
                }
                sub.push("    </table><br/>");
            }

            sub.push("  </div>")
            sub.push("  </fieldset>");

            html.push(sub.join(""));
        }

        html.push("</div>");
        return html.join("");
    }

}